package org.csu.mypetstoreplus.controller;

import org.csu.mypetstoreplus.common.BaseResponse;

import org.csu.mypetstoreplus.service.CatalogService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    //获取Category列表
    @GetMapping("/categories")
    public BaseResponse<Object> getCategories() {
        return BaseResponse.createForSuccess(catalogService.getCategories());
    }

    //获取指定Category下的Product列表
    @GetMapping("/categories/{id}/products")
    public BaseResponse<Object> getProductsByCategory(@PathVariable int id) {
        String categoryId = catalogService.getCategoryById(id).getCategoryId();
        return BaseResponse.createForSuccess(catalogService.getProductsByCategoryId(categoryId));
    }

    //获取指定Product下的Item列表
    @GetMapping("/products/{id}/items")
    public BaseResponse<Object> getItemsByProduct(@PathVariable int id) {
        String productId = catalogService.getProductById(id).getProductId();
        return BaseResponse.createForSuccess(catalogService.getItemVOsByProductId(productId));
    }

    //获取Product列表（用于搜索）
    @GetMapping("/products")
    public BaseResponse<Object> getProducts() {
        return BaseResponse.createForSuccess(catalogService.getProducts());
    }
}
